clear all
set more off
set maxvar 10000
version 13
set seed 285621959



*SET DIRECTORY
global directory ""

	
* Subfolder globals
* -----------------

	global data				"${directory}/01_data"
	global output			"${directory}/02_output"
	
	
* Stata tex
* -----------
do "${output}/stata-tex.do"					

	
* Non-official commands 
* ------------------------	
ssc install estrat



* Globals
* --------------
	

global unbalancedcov	
global hetero			bl_network_mayotte_hh bl_mig_willingness bl_working_age_adult_n bl_ihsconsopea bl_schooling bl_treated_tot_cont
global controls 		bl_hh_size bl_ihsconsopea bl_bank bl_iga_d bl_fields_d bl_tlu_n bl_privatewater bl_elec /// 
						bl_men bl_age bl_agesq bl_schooling_0 bl_schooling_1 bl_schooling_2 bl_schooling_3 ///
						bl_mig_willingness bl_network_mayotte_hh bl_mig_experience
global controls_indiv 	bl_age_indiv bl_agesq_indiv bl_male_indiv bl_schooling_indiv_0 bl_schooling_indiv_1 ///
						bl_schooling_indiv_2 bl_schooling_indiv_3 bl_willingness_indiv 


* Layout graphs
* -----------

global plotregion 	plotregion(color(white) fcolor(white) lcolor(white) icolor(white) ifcolor(white) ilcolor(white))  
global graphregion 	graphregion(color(white) fcolor(white) lcolor(white) icolor(white) ifcolor(white) ilcolor(white)) 
global legendregion region(lstyle(none) fcolor(white) lcolor(white) icolor(white) ifcolor(white) ilcolor(white)) size(medsmall) colgap(huge) keygap(vsmall) just(center) symxsize(huge)
 
global width="medthin"
global size="small"
global color1="gs0%70 maroon"
global color2="orange_red"
global colorCI="gs1%40"
global CI="color($colorCI) lwidth(`width') msize(`size')"

local width="medthin"
local size="small"
local color1="maroon" //  gs0%70 maroon
local color2="orange_red" //  gs10%70 orange_red
local colorCI="gs1%40"
local CI="color(`colorCI') lwidth(`width') msize(`size')"






* TABLE 1 
* --------

preserve
use "${data}/migration_prepared", clear

foreach var in $controls bl_survey_island_1 bl_survey_island_2 bl_survey_island_3 bl_debts {
	di "`var'"
	ttest `var' , by(treated)
	local mean1=`r(mu_1)'
	local mean2=`r(mu_2)'
	local diff = `r(mu_1)'-`r(mu_2)' 
	local sd1=`r(sd_1)'
	local sd2=`r(sd_2)'
	local n1=`r(N_1)'
	local n2=`r(N_2)'
	local ntot=`n1'+`n2'
	insert_into_file using sample_table.csv, key(bal_`var'_pval) 	value(" `r(p)'") format(%6.2f)
	insert_into_file using sample_table.csv, key(bal_`var'_MEAN1) 	value(" `mean1'") format(%6.2f)
	insert_into_file using sample_table.csv, key(bal_`var'_MEAN2)	value(" `mean2'") format(%6.2f)
	insert_into_file using sample_table.csv, key(bal_`var'_SD1) 	value(" `sd1'") format(%6.2f)
	insert_into_file using sample_table.csv, key(bal_`var'_SD2) 	value(" `sd2'") format(%6.2f)
	insert_into_file using sample_table.csv, key(bal_`var'_DIFF) 	value(" `diff'") format(%6.2f)
	insert_into_file using sample_table.csv, key(bal_`var'_N1) 		value(" `n1'") format(%-9.0g)
	insert_into_file using sample_table.csv, key(bal_`var'_N2) 		value(" `n2'") format(%-9.0g)
	insert_into_file using sample_table.csv, key(bal_`var'_NTOT) 	value(" `ntot'") format(%-9.0g)
}

reg treated $controls bl_survey_island_1 bl_survey_island_2 bl_survey_island_3 , vce(cluster hh_id)

restore

table_from_tpl, t(TPL_tab1.tex) r(sample_table.csv) o(tab1.tex)



* TABLE 2 
* ---------

preserve
use "${data}/migration_prepared", clear
drop if attrition==1
	global outcome 		cfw8 daysworked daysworkedall ihs_cfw_y ihs_totincome ihs_totincomeall
	
	foreach var in $outcome {
			noi di "`var'"
			reg `var' treated  , ro
			store_est_tpl using sample_table.csv , coef(_cons) name(cmean_`var') all
			}
	
	foreach var in $outcome {
		noi di "`var'"
		reg `var' treated $unbalancedcov , ro
		store_est_tpl using sample_table.csv , coef(treated) name(labor1_`var') all
		reg `var' treated $unbalancedcov $controls , ro
		store_est_tpl using sample_table.csv , coef(treated) name(labor2_`var') all
		reg `var' treated $unbalancedcov $controls i.survey_island , ro
		store_est_tpl using sample_table.csv , coef(treated) name(labor3_`var') all
		}

restore

table_from_tpl, t(TPL_tab2.tex) r(sample_table.csv) o(tab2.tex)

	

* TABLE 3 
* ---------

preserve
use "${data}/migration_prepared", clear
drop if attrition==1

foreach var in migmhr_dum migmhrall_dum {

	* Baseline specification	
	reg `var' treated  , ro
	store_est_tpl using sample_table.csv , coef(_cons) name(controlmean_`var') all
	reg `var' treated $unbalancedcov , vce(cluster hh_id)
	mat B=e(b)
	local coef = B[1,1]*100
	noi di `coef'
	store_est_tpl using sample_table.csv , coef(treated) name(`var'1) all
	insert_into_file using sample_table.csv, key(`var'1_writing) value(" `coef'") format(%6.1f)		
	
	* Extended controls
	reg `var' treated $unbalancedcov $controls  , ro
	store_est_tpl using sample_table.csv , coef(treated) name(`var'2) all

	* Island_FE
	reg `var' treated $unbalancedcov $controls i.survey_island , ro
	store_est_tpl using sample_table.csv , coef(treated) name(`var'3) all

	}
	
restore

table_from_tpl, t(TPL_tab3.tex) r(sample_table.csv) o(tab3.tex)
	



* TABLE 4
* -----------

preserve
use "${data}/migration_prepared", clear
drop if attrition==1

	foreach var in migmhr_dum migmhrall_dum {
			
			reg `var' treated##bl_liq_2mean   , ro
			store_est_tpl using sample_table.csv , coef(_cons) name(`var'_controlmean_liq_l) all
			
			reg `var' treated##bl_liq_2mean $unbalancedcov $controls , ro
			store_est_tpl using sample_table.csv , coef(1.treated) name(`var'_liqa_1) all
			store_est_tpl using sample_table.csv , coef(1.bl_liq_2mean) name(`var'_liqb_1) all
			store_est_tpl using sample_table.csv , coef(1.treated#1.bl_liq_2mean) name(`var'_liqc_1) all
			lincom 1.treated + 1.treated#1.bl_liq_2mean
			local coef = `r(estimate)'
			local se = `r(se)'
			local text : di %6.3f `coef'
			local pvalue = `r(p)'
			count_stars, p(`pvalue')
			insert_into_file using sample_table.csv, key(`var'_liqd_1_starbeta) value(" `text'`r(stars)'") format(%6.3f)
			insert_into_file using sample_table.csv, key(`var'_liqd_1_se) value(" `se'") format(%6.3f)

			reg `var' treated##bl_liq_2mean $unbalancedcov $controls i.survey_island   , ro
			store_est_tpl using sample_table.csv , coef(1.treated) name(`var'_liqa_2) all
			store_est_tpl using sample_table.csv , coef(1.bl_liq_2mean) name(`var'_liqb_2) all
			store_est_tpl using sample_table.csv , coef(1.treated#1.bl_liq_2mean) name(`var'_liqc_2) all
			lincom 1.treated + 1.treated#1.bl_liq_2mean
			local coef = `r(estimate)'
			local se = `r(se)'
			local text : di %6.3f `coef'
			local pvalue = `r(p)'
			count_stars, p(`pvalue')
			insert_into_file using sample_table.csv, key(`var'_liqd_2_starbeta) value(" `text'`r(stars)'") format(%6.3f)
			insert_into_file using sample_table.csv, key(`var'_liqd_2_se) value(" `se'") format(%6.3f)
			

			reg `var' treated##bl_liq_2mean $unbalancedcov $controls i.survey_island  liq2mean_* , ro
			store_est_tpl using sample_table.csv , coef(1.treated) name(`var'_liqa_3) all
			store_est_tpl using sample_table.csv , coef(1.bl_liq_2mean) name(`var'_liqb_3) all
			store_est_tpl using sample_table.csv , coef(1.treated#1.bl_liq_2mean) name(`var'_liqc_3) all
			lincom 1.treated + 1.treated#1.bl_liq_2mean
			local coef = `r(estimate)'
			local se = `r(se)'
			local text : di %6.3f `coef'
			local pvalue = `r(p)'
			count_stars, p(`pvalue')
			insert_into_file using sample_table.csv, key(`var'_liqd_3_starbeta) value(" `text'`r(stars)'") format(%6.3f)
			insert_into_file using sample_table.csv, key(`var'_liqd_3_se) value(" `se'") format(%6.3f)
			
			
			reg `var' treated##c.bl_liquidities   , ro
			store_est_tpl using sample_table.csv , coef(_cons) name(`var'_controlmean_liqcont_l) all
		
			reg `var' treated##c.bl_liquidities $unbalancedcov $controls , ro
			store_est_tpl using sample_table.csv , coef(1.treated) name(`var'_liqconta_1) all
			store_est_tpl using sample_table.csv , coef(bl_liquidities) name(`var'_liqcontb_1) all
			store_est_tpl using sample_table.csv , coef(1.treated#bl_liquidities) name(`var'_liqcontc_1) all
			
			reg `var' treated##c.bl_liquidities $unbalancedcov $controls i.survey_island  , ro
			store_est_tpl using sample_table.csv , coef(1.treated) name(`var'_liqconta_2) all
			store_est_tpl using sample_table.csv , coef(bl_liquidities) name(`var'_liqcontb_2) all
			store_est_tpl using sample_table.csv , coef(1.treated#bl_liquidities) name(`var'_liqcontc_2) all

			reg `var' treated##c.bl_liquidities $unbalancedcov $controls i.survey_island  liq_* , ro
			store_est_tpl using sample_table.csv , coef(1.treated) name(`var'_liqconta_3) all
			store_est_tpl using sample_table.csv , coef(bl_liquidities) name(`var'_liqcontb_3) all
			store_est_tpl using sample_table.csv , coef(1.treated#bl_liquidities) name(`var'_liqcontc_3) all


		}
		
restore		

table_from_tpl, t(TPL_tab4.tex) r(sample_table.csv) o(tab4.tex)		

		

* TABLE 5
* ----------

preserve
use "${data}/migration_prepared", clear
drop if attrition==1
drop cfw6
merge 1:m hh_id using "${data}/migration_indiv_prepared.dta" 
drop if _merge!=3
drop _merge

collapse (max) mig_financing_self mig_financing_debt mig_financing_help treated $unbalancedcov $controls survey_island , by(hh_id)

global migfinancing mig_financing_self mig_financing_debt mig_financing_help 

	foreach var in $migfinancing  {
		reg `var' treated  , ro
		store_est_tpl using sample_table.csv , coef(_cons) name(controlmean_`var') all
		reg `var' treated $unbalancedcov $controls i.survey_island , ro
		store_est_tpl using "sample_table.csv" , coef(treated) name(`var') all
			}

restore

table_from_tpl, t(TPL_tab5.tex) r(sample_table.csv) o(tab5.tex)
		
		

** TABLE 6
* -----------

preserve
use "${data}/migration_prepared", clear
drop if attrition==1
drop cfw6
	
	merge 1:m hh_id using "${data}/migration_indiv_prepared.dta" 
	keep migmhr* payment* paymenttot* treated* hh_id $controls survey_island
	collapse (max) migmhr* treated payment* $controls survey_island , by(hh_id)
	reshape long migmhr_time@ payment@ paymenttot@ migmhr_imput_time@ migmhrall_time@ migmhrall_imput_time@ , i(hh_id) j(T)
	replace payment=1 if payment==2
	xtset hh_id T

	foreach var in migmhr {
		
		* Control mean
		xtreg `var'_time treated , vce(cluster hh_id)
		store_est_tpl using sample_table.csv , coef(_cons) name(panel_controlmean_`var') all
	
		* No lag
		xtreg `var'_time L.`var'_time payment L.paymenttot $controls $unbalancedcov i.survey_island , vce(cluster hh_id) 
		store_est_tpl using sample_table.csv , coef(payment) name(panel_`var'1_payment) all format(%6.4f)
		store_est_tpl using sample_table.csv , coef(L.paymenttot) name(panel_`var'1_paymenttotL1) all format(%6.4f)
		store_est_tpl using sample_table.csv , coef(L.`var'_time) name(panel_`var'1_endoL1) all	format(%6.3f)
	
		* One lag
		xtreg `var'_time L.`var'_time payment L.payment L2.paymenttot $controls $unbalancedcov i.survey_island , vce(cluster hh_id)
		store_est_tpl using sample_table.csv , coef(payment) name(panel_`var'2_payment) all format(%6.4f)
		store_est_tpl using sample_table.csv , coef(L1.payment) name(panel_`var'2_paymentL1) all format(%6.4f)
		store_est_tpl using sample_table.csv , coef(L2.paymenttot) name(panel_`var'2_paymenttotL2) all format(%6.4f)
		store_est_tpl using sample_table.csv , coef(L1.`var'_time) name(panel_`var'2_endoL1) all format(%6.3f)
		
		* Interaction
		xtreg `var'_time L.`var'_time payment##C.L.paymenttot $controls $unbalancedcov i.survey_island , vce(cluster hh_id)
		store_est_tpl using sample_table.csv , coef(1.payment) name(panel_`var'3_payment) all format(%6.4f)
		store_est_tpl using sample_table.csv , coef(L.paymenttot) name(panel_`var'3_paymenttotL1) all	format(%6.4f)
		store_est_tpl using sample_table.csv , coef(1.payment#cL.paymenttot) name(panel_`var'3_inter) all	format(%6.4f)
		store_est_tpl using sample_table.csv , coef(L1.`var'_time) name(panel_`var'3_endoL1) all format(%6.3f)
		}

restore

table_from_tpl, t(TPL_tab6.tex) r(sample_table.csv) o(tab6.tex)



* TABLE 7
* ----------

preserve
use "${data}/migration_prepared", clear
drop if attrition==1
	
foreach var in migmhr_dum {
			
	reg `var' treated##bl_risk_averse   , ro
	store_est_tpl using sample_table.csv , coef(_cons) name(`var'_controlmean_risk_l) all
	
	reg `var' treated##bl_risk_averse $unbalancedcov $controls , ro
	store_est_tpl using sample_table.csv , coef(1.treated) name(`var'_riska_1) all
	store_est_tpl using sample_table.csv , coef(1.bl_risk_averse) name(`var'_riskb_1) all
	store_est_tpl using sample_table.csv , coef(1.treated#1.bl_risk_averse) name(`var'_riskc_1) all
	lincom 1.treated + 1.treated#1.bl_risk_averse
	local coef = `r(estimate)'
	local se = `r(se)'
	local text : di %6.3f `coef'
	local pvalue = `r(p)'
	count_stars, p(`pvalue')
	insert_into_file using sample_table.csv, key(`var'_riskd_1_starbeta) value(" `text'`r(stars)'") format(%6.3f)
	insert_into_file using sample_table.csv, key(`var'_riskd_1_se) value(" `se'") format(%6.3f)

	reg `var' treated##bl_risk_averse $unbalancedcov $controls i.survey_island   , ro
	store_est_tpl using sample_table.csv , coef(1.treated) name(`var'_riska_2) all
	store_est_tpl using sample_table.csv , coef(1.bl_risk_averse) name(`var'_riskb_2) all
	store_est_tpl using sample_table.csv , coef(1.treated#1.bl_risk_averse) name(`var'_riskc_2) all
	lincom 1.treated + 1.treated#1.bl_risk_averse
	local coef = `r(estimate)'
	local se = `r(se)'
	local text : di %6.3f `coef'
	local pvalue = `r(p)'
	count_stars, p(`pvalue')
	insert_into_file using sample_table.csv, key(`var'_riskd_2_starbeta) value(" `text'`r(stars)'") format(%6.3f)
	insert_into_file using sample_table.csv, key(`var'_riskd_2_se) value(" `se'") format(%6.3f)
	

	reg `var' treated##bl_risk_averse $unbalancedcov $controls i.survey_island  bl_risks_* , ro
	store_est_tpl using sample_table.csv , coef(1.treated) name(`var'_riska_3) all
	store_est_tpl using sample_table.csv , coef(1.bl_risk_averse) name(`var'_riskb_3) all
	store_est_tpl using sample_table.csv , coef(1.treated#1.bl_risk_averse) name(`var'_riskc_3) all
	lincom 1.treated + 1.treated#1.bl_risk_averse
	local coef = `r(estimate)'
	local se = `r(se)'
	local text : di %6.3f `coef'
	local pvalue = `r(p)'
	count_stars, p(`pvalue')
	insert_into_file using sample_table.csv, key(`var'_riskd_3_starbeta) value(" `text'`r(stars)'") format(%6.3f)
	insert_into_file using sample_table.csv, key(`var'_riskd_3_se) value(" `se'") format(%6.3f)
	
	
	reg `var' treated##c.bl_risk_aversion   , ro
	store_est_tpl using sample_table.csv , coef(_cons) name(`var'_controlmean_riskcont_l) all
	
	reg `var' treated##c.bl_risk_aversion $unbalancedcov $controls , ro
	store_est_tpl using sample_table.csv , coef(1.treated) name(`var'_riskconta_1) all
	store_est_tpl using sample_table.csv , coef(bl_risk_aversion) name(`var'_riskcontb_1) all
	store_est_tpl using sample_table.csv , coef(1.treated#bl_risk_aversion) name(`var'_riskcontc_1) all
	
	reg `var' treated##c.bl_risk_aversion $unbalancedcov $controls i.survey_island  , ro
	store_est_tpl using sample_table.csv , coef(1.treated) name(`var'_riskconta_2) all
	store_est_tpl using sample_table.csv , coef(bl_risk_aversion) name(`var'_riskcontb_2) all
	store_est_tpl using sample_table.csv , coef(1.treated#bl_risk_aversion) name(`var'_riskcontc_2) all

	reg `var' treated##c.bl_risk_aversion $unbalancedcov $controls i.survey_island  risk_* , ro
	store_est_tpl using sample_table.csv , coef(1.treated) name(`var'_riskconta_3) all
	store_est_tpl using sample_table.csv , coef(bl_risk_aversion) name(`var'_riskcontb_3) all
	store_est_tpl using sample_table.csv , coef(1.treated#bl_risk_aversion) name(`var'_riskcontc_3) all

	}

restore

table_from_tpl, t(TPL_tab7.tex) r(sample_table.csv) o(tab7.tex)
	
	
	
	
	
	
**********************************************************
******************** APPENDIX ****************************	
**********************************************************




* TABLE A2
* ---------

preserve
use "${data}/migration_prepared", clear
drop if attrition==1

	tab treated_survey treated, column
	bysort survey_island: tab treated_survey treated, column
	
	foreach var in migmhr_dum migmhrall_dum {
		
		* Baseline specification	
		reg `var' treated  , vce(cluster hh_id)
		store_est_tpl using sample_table.csv , coef(_cons) name(controlmean_`var') all
		ivregress 2sls `var' (treated_survey = treated) , cluster(hh_id)
		store_est_tpl using sample_table.csv , coef(treated) name(`var'iv1) all
		
		* Extended controls
		ivregress 2sls `var' $unbalancedcov $controls (treated_survey = treated), vce(cluster hh_id)
		store_est_tpl using sample_table.csv , coef(treated) name(`var'iv2) all
		
		* Island_FE
		ivregress 2sls `var' $unbalancedcov $controls i.survey_island (treated_survey = treated), vce(cluster hh_id)
		store_est_tpl using sample_table.csv , coef(treated) name(`var'iv3) all
		
		}

restore
		
table_from_tpl, t(TPL_tabA2.tex) r(sample_table.csv) o(tabA2.tex)





* TABLE A3
* ---------
		
use "${data}/migration_prepared", clear
drop if attrition==1

		sum migmhr_dum if treated==0 & income_all_b!=0
		local cmean1 = r(mean)
		insert_into_file using sample_table.csv, key(elast_cmean1) value(" `cmean1'") format(%6.3f)						
		sum migmhrall_dum if treated==0 & income_all_b!=0
		local cmean2 = r(mean)
		insert_into_file using sample_table.csv, key(elast_cmean2) value(" `cmean2'") format(%6.3f)						
		foreach var in migmhr_dum migmhrall_dum {
				sum `var' if treated==0 & income_all_b!=0
				scalar control_mean = r(mean)
				* Baseline specification	
				reg log_income_all treated $unbalancedcov if income_all_b!=0, ro 
				store_est_tpl using sample_table.csv , coef(treated) name(s1_`var'1) all
				ivreg2 `var' $unbalancedcov (log_income_all = treated) if income_all_b!=0, ro first 
				local fstat = e(rkf)
				insert_into_file using sample_table.csv, key(ftest_`var'1) value(" `fstat'") format(%6.2f)	
				store_est_tpl using sample_table.csv , coef(log_income_all) name(s2_`var'1) all
				local elasticity =round(_b[log_income_all]/control_mean,0.001)
				insert_into_file using sample_table.csv, key(elast_`var'1) value(" `elasticity'") format(%6.2f)			
				* Extended controls
				reg log_income_all treated $unbalancedcov $controls if income_all_b!=0, ro 
				store_est_tpl using sample_table.csv , coef(treated) name(s1_`var'2) all
				ivreg2 `var' $unbalancedcov $controls (log_income_all = treated) if income_all_b!=0, ro first 
				local fstat = e(rkf)
				insert_into_file using sample_table.csv, key(ftest_`var'2) value(" `fstat'") format(%6.2f)	
				store_est_tpl using sample_table.csv , coef(log_income_all) name(s2_`var'2) all
				local elasticity =round(_b[log_income_all]/control_mean,0.001)
				insert_into_file using sample_table.csv, key(elast_`var'2) value(" `elasticity'") format(%6.2f)	
				* Island_FE
				reg log_income_all treated $unbalancedcov $controls  i.survey_island if income_all_b!=0, ro 
				store_est_tpl using sample_table.csv , coef(treated) name(s1_`var'3) all
				ivreg2 `var' $unbalancedcov $controls i.survey_island (log_income_all = treated) if income_all_b!=0, ro first 
				local fstat = e(rkf)
				insert_into_file using sample_table.csv, key(ftest_`var'3) value(" `fstat'") format(%6.2f)	
				store_est_tpl using sample_table.csv , coef(log_income_all) name(s2_`var'3) all
				local elasticity =round(_b[log_income_all]/control_mean,0.001)
				insert_into_file using sample_table.csv, key(elast_`var'3) value(" `elasticity'") format(%6.2f)	
		}
		
	
table_from_tpl, t(TPL_tabA3.tex) r(sample_table.csv) o(tabA3.tex)





	
* TABLE A4 
* --------
 
preserve
use "${data}/migration_prepared", clear
drop if attrition==1

	merge 1:m hh_id using "${data}/migration_indiv_prepared.dta" , force
	drop if _merge!=3
	drop _merge
	
	global remitt left_rem1 left_rem2
	collapse (max) treated $controls survey_village $remitt , by(hh_id)

	foreach var in $remitt {
			noi di "`var'"
			reg `var' treated  , ro
			store_est_tpl using sample_table.csv , coef(_cons) name(cmean_`var') all
	}
	
	foreach var in $remitt {
			noi di "`var'"
			reg `var' treated $unbalancedcov , ro
			store_est_tpl using sample_table.csv , coef(treated) name(`var'_1) all
			reg `var' treated $unbalancedcov $controls , cluster(hh_id)
			store_est_tpl using sample_table.csv , coef(treated) name(`var'_2) all
			reg `var' treated $unbalancedcov $controls i.survey_village  , cluster(hh_id)
			store_est_tpl using sample_table.csv , coef(treated) name(`var'_3) all
	}

restore	

table_from_tpl, t(TPL_tabA4.tex) r(sample_table.csv) o(tabA4.tex)




* FIGURE A2
* ----------

preserve
use "${data}/migration_prepared", clear
drop if attrition==1
#delimit ;
twoway (fpfitci remittances left_year),
legen(off)
xsize(3.5)
xtitle("Year of migration", size(medlarge)) 
xlabel(2000 "2000" 2005 "2005" 2010 "2010" 2015 "2015" 2018 "2018" , nogrid labsize(3))
ytitle("Remittances", size(medlarge))
ylabel(0(0.2)1, nogrid labsize(3))
$plotregion $graphregion
;
#delimit cr	
graph export "${output}\figA2.pdf", as(pdf) replace
restore
		
		

		

* TABLE A5 
* -----------

preserve
use "${data}/migration_prepared", clear
	
	ttest attrition , by(treated)

	local mean1=`r(mu_1)'
	local mean2=`r(mu_2)'
	local n1=`r(N_1)'
	local n2=`r(N_2)'
	local ntot=`n1'+`n2'
	local diff = `r(mu_1)'-`r(mu_2)' 
	local sd1=`r(sd_1)'
	local sd2=`r(sd_2)'
	
	insert_into_file using sample_table.csv, key(attri_pval) 	value(" `r(p)'") format(%6.2f)
	insert_into_file using sample_table.csv, key(attri_N1) 		value(" `n1'") format(%-9.0g)
	insert_into_file using sample_table.csv, key(attri_N2) 		value(" `n2'") format(%-9.0g)
	insert_into_file using sample_table.csv, key(attri_NTOT) 	value(" `ntot'") format(%-9.0g)
	insert_into_file using sample_table.csv, key(attri_MEAN1) 	value(" `mean1'") format(%6.3f)
	insert_into_file using sample_table.csv, key(attri_MEAN2)	value(" `mean2'") format(%6.3f)
	insert_into_file using sample_table.csv, key(attri_SD1) 	value(" `sd1'") format(%6.3f)
	insert_into_file using sample_table.csv, key(attri_SD2) 	value(" `sd2'") format(%6.3f)
	insert_into_file using sample_table.csv, key(attri_DIFF) 	value(" `diff'") format(%6.3f)
	
restore

table_from_tpl, t(TPL_tabA5.tex) r(sample_table.csv) o(tabA5.tex)



			
* TABLE A6
* ----------

preserve
use "${data}/migration_prepared", clear

forvalues x=2/7 {
	ttest attrition_why_`x' , by(treated)
	local mean1=`r(mu_1)'
	local mean2=`r(mu_2)'
	local diff = `r(mu_1)'-`r(mu_2)' 
	local sd1=`r(sd_1)'
	local sd2=`r(sd_2)'
	insert_into_file using sample_table.csv, key(attri`x'_pval) 	value(" `r(p)'") format(%6.2f)
	insert_into_file using sample_table.csv, key(attri`x'_MEAN1) 	value(" `mean1'") format(%6.3f)
	insert_into_file using sample_table.csv, key(attri`x'_MEAN2)	value(" `mean2'") format(%6.3f)
	insert_into_file using sample_table.csv, key(attri`x'_SD1) 		value(" `sd1'") format(%6.3f)
	insert_into_file using sample_table.csv, key(attri`x'_SD2) 		value(" `sd2'") format(%6.3f)
	insert_into_file using sample_table.csv, key(attri`x'_DIFF) 	value(" `diff'") format(%6.3f)
}

restore

table_from_tpl, t(TPL_tabA6.tex) r(sample_table.csv) o(tabA6.tex)



* TABLE A7 
* ----------

preserve
use "${data}/migration_prepared", clear
drop if attrition==1

foreach var in migmhr_dum migmhrall_dum {

	* Baseline specification	
	reg `var' treated##intensity40 , vce(cluster survey_village)
	store_est_tpl using sample_table.csv , coef(_cons) name(`var'controlmean_spl) all
	reg `var' treated##intensity40  $unbalancedcov , vce(cluster survey_village)
	store_est_tpl using sample_table.csv , coef(1.treated) name(`var'treated_spl1) all
	store_est_tpl using sample_table.csv , coef(1.intensity40) name(`var'itec1) all
	store_est_tpl using sample_table.csv , coef(1.treated#1.intensity40) name(`var'inter1) all
	lincom 1.intensity40 + 1.treated#1.intensity40
	local coef = `r(estimate)'
	local se = `r(se)'
	local text : di %6.3f `coef'
	local pvalue = `r(p)'
	count_stars, p(`pvalue')
	insert_into_file using sample_table.csv, key(`var'itet1_starbeta) value(" `text'`r(stars)'") format(%6.3f)
	insert_into_file using sample_table.csv, key(`var'itet1_se) value(" `se'") format(%6.3f)

		
	* Extended controls
	reg `var' treated##intensity40 $unbalancedcov $controls , vce(cluster survey_village)
	store_est_tpl using sample_table.csv , coef(1.treated) name(`var'treated_spl2) all
	store_est_tpl using sample_table.csv , coef(1.intensity40) name(`var'itec2) all
	store_est_tpl using sample_table.csv , coef(1.treated#1.intensity40) name(`var'inter2) all
	lincom 1.intensity40 + 1.treated#1.intensity40
	local coef = `r(estimate)'
	local se = `r(se)'
	local text : di %6.3f `coef'
	local pvalue = `r(p)'
	count_stars, p(`pvalue')
	insert_into_file using sample_table.csv, key(`var'itet2_starbeta) value(" `text'`r(stars)'") format(%6.3f)
	insert_into_file using sample_table.csv, key(`var'itet2_se) value(" `se'") format(%6.3f)

	* Island_FE
	reg `var' treated##intensity40 $unbalancedcov $controls i.survey_island , vce(cluster hh_id)
	store_est_tpl using sample_table.csv , coef(1.treated) name(`var'treated_spl3) all
	store_est_tpl using sample_table.csv , coef(1.intensity40) name(`var'itec3) all
	store_est_tpl using sample_table.csv , coef(1.treated#1.intensity40) name(`var'inter3) all
	lincom 1.intensity40 + 1.treated#1.intensity40
	local coef = `r(estimate)'
	local se = `r(se)'
	local text : di %6.3f `coef'
	local pvalue = `r(p)'
	count_stars, p(`pvalue')
	insert_into_file using sample_table.csv, key(`var'itet3_starbeta) value(" `text'`r(stars)'") format(%6.3f)
	insert_into_file using sample_table.csv, key(`var'itet3_se) value(" `se'") format(%6.3f)	


}

restore

table_from_tpl, t(TPL_tabA7.tex) r(sample_table.csv) o(tabA7.tex)






* TABLE A8 
* ----------

preserve
use "${data}/migration_prepared", clear
drop if attrition==1
		
		global all						ihs_totincomeall daysworkedall ihs_totalfoodexp ihs_totalnonfoodexp foodsecindex assetindex2 ///
										financialindex migmhr_dum sopoindex conflictindex womanempowerindex psyindex

		keep hh_id treated $unbalancedcov $controls survey_island $all
			
		foreach var in $all {
				noi di "`var'"
				reg `var' treated  , cluster(hh_id)
				store_est_tpl using "sample_table.csv" , coef(_cons) name(controlmean_`var') all
		}
		
		local i=1
		gen pvalue=.
		gen varname=""
		
		foreach var in $all {
				noi di "`var'"
				replace varname="`var'" if _n==`i'
				reg `var' treated $unbalancedcov $controls i.survey_island , cluster(hh_id)
				store_est_tpl using "sample_table.csv" , coef(treated) name(directeffects_`var') all
				local t = _b[treated]/_se[treated]
				local p =2*ttail(e(df_r),abs(`t'))
				replace pvalue=`p' if _n==`i'
				insert_into_file using sample_table.csv, key(directeffects_`var'_pval) value(" `p'") format(%6.3f)
				local ++i
		}
		
		drop if pvalue==. 
		keep pvalue varname
		// br pvalue varname
					
		tempfile temp
		save `temp', replace
		
		** 12 outcomes
		do "${data}/fdr_sharpened_qvalues.do"
		foreach var in $all {
		sum bky06_qval if varname=="`var'"
		insert_into_file using sample_table.csv, key(directeffects_`var'_qval_12) value(" `r(mean)'") format(%6.3f)
		}
		
		** 8 outcomes
		use "`temp'", clear 
		keep if _n<=8
		do "${data}/fdr_sharpened_qvalues.do"
		foreach var in ihs_totincomeall daysworkedall  ihs_totalfoodexp ihs_totalnonfoodexp foodsecindex assetindex2 financialindex migmhr_dum {
		sum bky06_qval if varname=="`var'"
		insert_into_file using sample_table.csv, key(directeffects_`var'_qval_8) value(" `r(mean)'") format(%6.3f)
		}

restore
		
table_from_tpl, t(TPL_tabA8.tex) r("sample_table.csv") o(tabA8.tex)





* FIGURE A3
*------------

preserve
use "${data}/migration_prepared", clear
drop if attrition==1

		
	local x="migmhr_dum"			
			
			replace cat=. 
			replace mean_control=.
			replace mean_effect=.
			replace high=.
			replace low=.
			replace cat=0.65 if _n==1
			replace cat=1.45 if _n==2
			sum `x' if treated==0 & bl_liq_2mean==0
			replace mean_control=r(mean)*100 if _n==1 
			sum `x' if treated==0 & bl_liq_2mean==1
			replace mean_control=r(mean)*100 if _n==2
			reg `x' treated##bl_liq_2mean $unbalancedcov $controls i.survey_island  liq2mean_* , ro
			lincom 1.treated
			replace mean_effect = `r(estimate)'*100 if _n==1
			replace high=`r(ub)'*100 if _n==1
			replace low=`r(lb)'*100 if _n==1
			lincom 1.treated + 1.treated#1.bl_liq_2mean 	
			replace mean_effect = `r(estimate)'*100  if _n==2
			replace high=`r(ub)'*100 if _n==2
			replace low=`r(lb)'*100  if _n==2	
			foreach var in mean_effect high low  {
			replace `var'= mean_control + `var'
			}
			#delimit ;
			twoway   (bar mean_effect cat, color(`color2') barwidth(0.5)) (bar mean_control cat, color(`color1') barwidth(0.40)) 
			(rcap high low cat, `CI') ,   
			legend(order(2 1 3 )  label(2 "Control (C) mean") label(1 "C mean + TE") label(3 "TE 95% CI"))
			xsize(3)
			xlabel( 0 " " 0.65 "Low" 1.45 "High"  2 " " 0.65 `" " "  "Savings" "' 1.45 `" " "  "Savings" "' , noticks labsize(4)) 
			xtitle(" ", size(large))
			ytitle("Migration", size(large)) 
			ylabel(0(5)20, nogrid labsize(4))
			$plotregion $graphregion
			;
			#delimit cr
			graph export "${output}/figA3.pdf", as(pdf) replace
	
restore




* FIGURE A4
* ----------
	
preserve
use "${data}/migration_prepared", clear
drop if attrition==1
	
			local inter ="bl_liq_3gr"
			local inter_controls="liq3gr_"
			local x="migmhr_dum"
			replace cat=0.65 if _n==1
			replace cat=1.45 if _n==2
			replace cat=2.25 if _n==3
			sum `x' if treated==0 & `inter'==0
			replace mean_control=r(mean)*100 if _n==1 
			sum `x' if treated==0 & `inter'==1
			replace mean_control=r(mean)*100 if _n==2
			sum `x' if treated==0 & `inter'==2
			replace mean_control=r(mean)*100 if _n==3
			reg `x' treated##i.`inter' $controls $unbalancedcov i.survey_island `inter_controls'* , ro
			lincom 1.treated
			replace mean_effect = `r(estimate)'*100 if _n==1
			replace high=`r(ub)'*100 if _n==1
			replace low=`r(lb)'*100 if _n==1
			lincom 1.treated + 1.treated#1.`inter' 	
			replace mean_effect = `r(estimate)'*100  if _n==2
			replace high=`r(ub)'*100 if _n==2
			replace low=`r(lb)'*100  if _n==2
			lincom 1.treated + 1.treated#2.`inter' 	
			replace mean_effect = `r(estimate)'*100  if _n==3
			replace high=`r(ub)'*100 if _n==3
			replace low=`r(lb)'*100  if _n==3	
			foreach var in mean_effect high low {
			replace `var'= mean_control + `var'
			replace `var'= 0 if `var' < 0
			}
			#delimit ;
			twoway   (bar mean_effect cat, color(`color2') barwidth(0.5)) (bar mean_control cat, color(`color1') barwidth(0.4)) 
			(rcap high low cat, $CI) ,    
			legend(order(2 1 3 )  label(2 "C mean") label(1 "C mean + TE") label(3 "TE 95% CI"))
			xsize(3)
			xlabel( 0 " " 0.65 "Low" 1.45 "Medium"  2.25 "High" 2.90 " " 0.65  `" " "  "Savings" "' 1.45 `" " "  "Savings" "' 2.25 `" " "  "Savings" "' , noticks labsize(4)) 
			xtitle(" ", size(medlarge))
			ytitle("Migration", size(medlarge)) 
			ylabel(0(5)20 , nogrid labsize(4))
			$plotregion $graphregion
			;
			#delimit cr
			graph export "${output}/figA4.pdf", as(pdf) replace
			
restore




* TABLE A9
* ----------

preserve
use "${data}/migration_prepared", clear
drop if attrition==1

foreach var in dom_mig_intra dom_mig_inter migfr_dum migother_dum {

	* Baseline specification	
	reg `var' treated  , ro
	store_est_tpl using sample_table.csv , coef(_cons) name(controlmean_`var') all
	reg `var' treated $unbalancedcov , vce(cluster hh_id)
	mat B=e(b)
	local coef = B[1,1]*100
	noi di `coef'
	store_est_tpl using sample_table.csv , coef(treated) name(`var'1) all
	insert_into_file using sample_table.csv, key(`var'1_writing) value(" `coef'") format(%6.1f)		
	
	* Extended controls
	reg `var' treated $unbalancedcov $controls , ro
	store_est_tpl using sample_table.csv , coef(treated) name(`var'2) all

	* Island_FE
	reg `var' treated $unbalancedcov $controls i.survey_island , ro
	store_est_tpl using sample_table.csv , coef(treated) name(`var'3) all

	}
	
restore

table_from_tpl, t(TPL_tabA9.tex) r(sample_table.csv) o(tabA9.tex)

	
	
	

* TABLE A10 
* ----------

preserve
use "${data}/migration_prepared", clear
drop if attrition==1
drop cfw6
merge 1:m hh_id using "${data}/migration_indiv_prepared.dta" 
drop if _merge!=3
drop _merge

foreach var in $controls_indiv bl_mig_experience_indiv bl_iga_indiv $hetero {
		sum `var' if cfw6==1 & treated!=0 & migmhrall!=1 & bl_age_indiv>=16 & bl_age_indiv<=65
		local n1=`r(N)'
		insert_into_file using sample_table.csv, key(`var'_mean1) 	value(" `r(mean)'") format(%6.2f)
		insert_into_file using sample_table.csv, key(`var'_N1) 	value(" `n1'") format(%-9.0g)
		sum `var' if cfw6==0 & treated!=0 & migmhrall!=1 & bl_age_indiv>=16 & bl_age_indiv<=65
		local n2=`r(N)'
		insert_into_file using sample_table.csv, key(`var'_mean2) 	value(" `r(mean)'") format(%6.2f)
		insert_into_file using sample_table.csv, key(`var'_N2) 	value(" `n2'") format(%-9.0g)
		sum `var' if treated!=0 & migmhrall==1 & bl_age_indiv>=16 & bl_age_indiv<=65 
		local n3=`r(N)'
		insert_into_file using sample_table.csv, key(`var'_mean3) 	value(" `r(mean)'") format(%6.2f)
		insert_into_file using sample_table.csv, key(`var'_N3) 	value(" `n3'") format(%-9.0g)
		sum `var' if treated!=0 & bl_age_indiv>=16 & bl_age_indiv<=65 
		local n4=`r(N)'
		insert_into_file using sample_table.csv, key(`var'_mean4) 	value(" `r(mean)'") format(%6.2f)
		insert_into_file using sample_table.csv, key(`var'_N4) 	value(" `n4'") format(%-9.0g)
		sum `var' if treated==0 & migmhrall==1 & bl_age_indiv>=16 & bl_age_indiv<=65
		local n5=`r(N)'
		insert_into_file using sample_table.csv, key(`var'_mean5) 	value(" `r(mean)'") format(%6.2f)
		insert_into_file using sample_table.csv, key(`var'_N5) 	value(" `n5'") format(%-9.0g)
		}
			
gen indiv_cat=1 if cfw6==1 & treated!=0 & migmhrall!=1 & bl_age_indiv>=16 & bl_age_indiv<=65
replace indiv_cat=2 if cfw6==0 & treated!=0 & migmhrall!=1 & bl_age_indiv>=16 & bl_age_indiv<=65
replace indiv_cat=3 if treated!=0 & migmhrall==1 & bl_age_indiv>=16 & bl_age_indiv<=65 
replace indiv_cat=4 if treated==0 & migmhrall==1 & bl_age_indiv>=16 & bl_age_indiv<=65
foreach var in $controls_indiv bl_mig_experience_indiv bl_iga_indiv $hetero {
		** 1-2
		mean `var' , over(indiv_cat)
		lincom [`var']1 - [`var']2
		local coef = `r(estimate)'
		local se = `r(se)'
		local pvalue = `r(p)'
		local text : di %6.2f `pvalue'
		count_stars, p(`pvalue')
		insert_into_file using sample_table.csv, key(tab6_`var'_diff12_pvalue) value(" `pvalue'") format(%6.2f)			
		insert_into_file using sample_table.csv, key(tab6_`var'_diff12) value(" `text'`r(stars)'") format(%6.2f)
		insert_into_file using sample_table.csv, key(tab6_`var'_diff12_se) value(" `se'") format(%6.2f)
		** 1-3
		lincom [`var']1 - [`var']3
		local coef = `r(estimate)'
		local se = `r(se)'
		local pvalue = `r(p)'
		local text : di %6.2f `pvalue'
		count_stars, p(`pvalue')
		insert_into_file using sample_table.csv, key(tab6_`var'_diff13_pvalue) value(" `pvalue'") format(%6.2f)			
		insert_into_file using sample_table.csv, key(tab6_`var'_diff13) value(" `text'`r(stars)'") format(%6.2f)
		insert_into_file using sample_table.csv, key(tab6_`var'_diff13_se) value(" `se'") format(%6.2f)
		** 1-4
		lincom [`var']1 - [`var']4
		local coef = `r(estimate)'
		local se = `r(se)'
		local pvalue = `r(p)'
		local text : di %6.2f `pvalue'
		count_stars, p(`pvalue')
		insert_into_file using sample_table.csv, key(tab6_`var'_diff14_pvalue) value(" `pvalue'") format(%6.2f)			
		insert_into_file using sample_table.csv, key(tab6_`var'_diff14) value(" `text'`r(stars)'") format(%6.2f)
		insert_into_file using sample_table.csv, key(tab6_`var'_diff14_se) value(" `se'") format(%6.2f)			
		** 2-3
		lincom [`var']2 - [`var']3
		local coef = `r(estimate)'
		local se = `r(se)'
		local pvalue = `r(p)'
		local text : di %6.2f `pvalue'
		count_stars, p(`pvalue')
		insert_into_file using sample_table.csv, key(tab6_`var'_diff23_pvalue) value(" `pvalue'") format(%6.2f)			
		insert_into_file using sample_table.csv, key(tab6_`var'_diff23) value(" `text'`r(stars)'") format(%6.2f)
		insert_into_file using sample_table.csv, key(tab6_`var'_diff23_se) value(" `se'") format(%6.2f)
		** 2-4
		lincom [`var']2 - [`var']4
		local coef = `r(estimate)'
		local se = `r(se)'
		local pvalue = `r(p)'
		local text : di %6.2f `pvalue'
		count_stars, p(`pvalue')
		insert_into_file using sample_table.csv, key(tab6_`var'_diff24_pvalue) value(" `pvalue'") format(%6.2f)			
		insert_into_file using sample_table.csv, key(tab6_`var'_diff24) value(" `text'`r(stars)'") format(%6.2f)
		insert_into_file using sample_table.csv, key(tab6_`var'_diff24_se) value(" `se'") format(%6.2f)
		** 3-4
		lincom [`var']4 - [`var']3
		local coef = `r(estimate)'
		local se = `r(se)'
		local pvalue = `r(p)'
		local text : di %6.2f `pvalue'
		count_stars, p(`pvalue')
		insert_into_file using sample_table.csv, key(tab6_`var'_diff34_pvalue) value(" `pvalue'") format(%6.2f)			
		insert_into_file using sample_table.csv, key(tab6_`var'_diff34) value(" `text'`r(stars)'") format(%6.2f)
		insert_into_file using sample_table.csv, key(tab6_`var'_diff34_se) value(" `se'") format(%6.2f)
}	

restore

table_from_tpl, t(TPL_tabA10.tex) r(sample_table.csv) o(tabA10.tex)	

	

	

* TABLE A11 
* ----------

preserve
use "${data}/migration_prepared", clear
drop if attrition==1

	sum debt3
	replace debt3=`r(mean)' if debt3==.d | debt3==.r
	replace debt3=0 if debt3==.
	replace debt3=asinh(debt3)
	global outcome debt3
		foreach var in $outcome {
				noi di "`var'"
				reg `var' treated  , ro
				store_est_tpl using sample_table.csv , coef(_cons) name(cmean_`var') all
				}
		
		foreach var in $outcome {
			noi di "`var'"
			reg `var' treated $unbalancedcov , ro
			store_est_tpl using sample_table.csv , coef(treated) name(`var'1) all
			reg `var' treated $unbalancedcov $controls , ro
			store_est_tpl using sample_table.csv , coef(treated) name(`var'2) all
			reg `var' treated $unbalancedcov $controls i.survey_island , ro
			store_est_tpl using sample_table.csv , coef(treated) name(`var'3) all
			}
restore

table_from_tpl, t(TPL_tabA11.tex) r(sample_table.csv) o(tabA11.tex)




	
* FIGURE A5
* ---------
	
preserve
use "${data}/migration_prepared", clear
drop if attrition==1
drop cfw6
merge 1:m hh_id using "${data}/migration_indiv_prepared.dta" 
drop if _merge!=3
drop _merge
collapse (max) migmhr* treated payment* $controls survey_island mean_control mean_effect high low cat cat1 cat2, by(hh_id)
	
	* Figure
		gen mean_paymenttot=.
		gen mean_paymentbefore=.
		gen mean_payment=.
		replace mean_control=.
		replace mean_effect=.
		replace high=.
		replace low=.	
				
		foreach outcome in migmhr {
			forvalues x=1/9 {
			replace cat=`x'*1.5 if _n==`x'
			replace cat1=(`x'*3)-1.5 if _n==`x'
			replace cat2=cat1+1 if _n==`x'
			sum `outcome'_time`x' if treated ==0 
			replace mean_control=r(mean)*100 if _n==`x'
			reg `outcome'_time`x'  treated $controls $unbalancedcov i.survey_island , vce(cluster hh_id) 
			matrix define A=r(table)
			replace mean_effect = A[1,1]*100 if _n==`x'
			replace high=A[6,1]*100 if _n==`x'
			replace low=A[5,1]*100 if _n==`x'
			sum paymenttot`x' if treated==1
			replace mean_paymenttot=r(mean)*20*2.3 if _n==`x'
			sum payment`x' if treated==1
			replace mean_payment=r(mean)*20*2.3 if _n==`x'
			replace mean_paymentbefore=mean_paymenttot-mean_payment if _n==`x'
			}
			order cat cat1 cat2 mean_control mean_effect high low mean_paymenttot mean_payment mean_paymentbefore			
			foreach var in mean_effect high low {
			replace `var'= mean_control + `var'
			replace `var'= 0 if `var' < 0
			}
			#delimit ;
			twoway   
			(bar mean_effect cat1, color(`color2') yaxis(1) barwidth(1.25)) (bar mean_control cat1, yaxis(1) color(`color1') barwidth(0.75)) 
			(rcap high low cat1, color(gs5) yaxis(1) lwidth(medthin) msize(small))
			(spike mean_paymenttot cat2, yaxis(2) lwidth(thick) color(gs12)) (spike mean_paymentbefore cat2, yaxis(2) lwidth(thick) color(gs4)) ,   
			xtitle("")  
			xlabel( 2 "Q3" 5 "Q4" 8 "Q1" 11 "Q2" 14 "Q3" 17 "Q4" 20 "Q1" 23 "Q2" 26 "Q3" 28 " " 0 " " 2 `" " "  "2016" "' 5 `" " "  "2016" "' 8 `" " "  "2017" "' 11 `" " "  "2017" "' 14 `" " "  "2017" "' 17 `" " "  "2017" "' 20 `" " "  "2018" "' 23 `" " "  "2018" "' 26 `" " "  "2018" "' , noticks labsize(3))   
			ytitle("Migration", size(medsmall) axis(1)) 
			ylabel(0(5)10, axis(1) labsize(3))  plotregion(color(white) fcolor(white) lcolor(white) icolor(white) ifcolor(white) ilcolor(white)) graphregion(color(white) fcolor(white) lcolor(white) icolor(white) ifcolor(white) ilcolor(white)) 
			ytitle("CFW received (in USD)", size(medsmall) axis(2)) 
			ylabel(0(50)250, axis(2) labsize(3)) 
			legend(order(1 5 2 4 3)  label(2 "C mean") label(1 "C mean + TE") label(4 "New CFW received") label(5 "Cumul. CFW received") label(3 "TE 95% CI") region(lstyle(none) fcolor(white) lcolor(white) icolor(white) ifcolor(white) ilcolor(white)) size(medsmall) colgap(huge) keygap(vsmall) just(center) symxsize(huge));
			#delimit cr
			graph export "${output}/figA5.pdf", as(pdf) replace
		}

restore



* FIGURE A6
* -----------

preserve
use "${data}/migration_prepared", clear
drop if attrition==1

	local x="migmhr_dum"
	replace cat=. 
	replace mean_control=.
	replace mean_effect=.
	replace high=.
	replace low=.
	replace cat=0.65 if _n==1
	replace cat=1.45 if _n==2		
	sum `x' if treated==0 & bl_risk_averse==0
	replace mean_control=r(mean)*100 if _n==1 
	sum `x' if treated==0 & bl_risk_averse==1
	replace mean_control=r(mean)*100 if _n==2
	reg `x' treated##bl_risk_averse $unbalancedcov $controls i.survey_island bl_risks_*  , ro
	lincom 1.treated
	replace mean_effect = `r(estimate)'*100 if _n==1
	replace high=`r(ub)'*100 if _n==1
	replace low=`r(lb)'*100 if _n==1
	lincom 1.treated + 1.treated#1.bl_risk_averse 	
	replace mean_effect = `r(estimate)'*100  if _n==2
	replace high=`r(ub)'*100 if _n==2
	replace low=`r(lb)'*100  if _n==2	
	foreach var in mean_effect high low  {
	replace `var'= mean_control + `var'
	}
	#delimit ;
	twoway   (bar mean_effect cat, color(`color2') barwidth(0.5))	(bar mean_control cat, color(`color1') barwidth(0.40)) 
	(rcap high low cat, `CI') ,   
	legend(order(2 1 3 )  label(2 "C mean") label(1 "C mean + TE") label(3 "TE 95% CI"))
	xsize(3)
	xlabel( 0 " " 0.65 "Low" 1.45 "High"  2 " " 0.65 `" " "  "Risk Aversion" "' 1.45 `" " "  "Risk Aversion" "' , noticks labsize(4)) 
	xtitle(" ", size(large))
	ytitle("Migration", size(large)) 
	ylabel(0(5)30, nogrid labsize(4))
	$plotregion $graphregion
	;
	#delimit cr
	graph export "${output}/figA6.pdf", as(pdf) replace
			
restore
	


	
* FIGURE A7 
* -----------

preserve
use "${data}/migration_prepared", clear
drop if attrition==1
sum bl_risk_tercile
	
	local inter ="bl_risk_tercile"
	local inter_controls="risk_terc_"
	local x="migmhr_dum"
			replace cat=0.65 if _n==1
			replace cat=1.45 if _n==2
			replace cat=2.25 if _n==3
			sum `x' if treated==0 & `inter'==0
			replace mean_control=r(mean)*100 if _n==1 
			sum `x' if treated==0 & `inter'==1
			replace mean_control=r(mean)*100 if _n==2
			sum `x' if treated==0 & `inter'==2
			replace mean_control=r(mean)*100 if _n==3
			reg `x' treated##i.`inter' $controls $unbalancedcov i.survey_island `inter_controls'* , ro
			lincom 1.treated
			replace mean_effect = `r(estimate)'*100 if _n==1
			replace high=`r(ub)'*100 if _n==1
			replace low=`r(lb)'*100 if _n==1
			lincom 1.treated + 1.treated#1.`inter' 	
			replace mean_effect = `r(estimate)'*100  if _n==2
			replace high=`r(ub)'*100 if _n==2
			replace low=`r(lb)'*100  if _n==2
			lincom 1.treated + 1.treated#2.`inter' 	
			replace mean_effect = `r(estimate)'*100  if _n==3
			replace high=`r(ub)'*100 if _n==3
			replace low=`r(lb)'*100  if _n==3	
			foreach var in mean_effect high low {
			replace `var'= mean_control + `var'
			replace `var'= 0 if `var' < 0
			}
			#delimit ;
			twoway   (bar mean_effect cat, color(`color2') barwidth(0.5)) (bar mean_control cat, color(`color1') barwidth(0.4)) 
			(rcap high low cat, $CI) ,    
			legend(order(2 1 3 )  label(2 "C mean") label(1 "C mean + TE") label(3 "TE 95% CI"))
			xsize(3)
			xlabel( 0 " " 0.65 "Low" 1.45 "Medium"  2.25 "High" 2.90 " " 0.65  `" " "  "Risk Av." "' 1.45 `" " "  "Risk Av." "' 2.25 `" " "  "Risk Av." "' , noticks labsize(4)) 
			xtitle(" ", size(medlarge))
			ytitle("Migration", size(medlarge)) 
			ylabel(0(5)30 , nogrid labsize(4))
			$plotregion $graphregion
			;
			#delimit cr
			graph export "${output}/figA7.pdf", as(pdf) replace
			
restore


	

	
* TABLE A12 
* ----------
 
preserve
use "${data}/migration_prepared", clear
drop if attrition==1


	global migwhy 	migmhr_why1 migmhr_why2 migmhr_why3 migmhr_why4 migmhr_why5 migmhr_why6 migmhr_why7 migmhrall_why1 migmhrall_why2 migmhrall_why3 migmhrall_why4 migmhrall_why5 migmhrall_why6 migmhrall_why7 
	collapse (max) migmhr_why* migmhrall_why* treated $controls survey_village , by(hh_id)

	foreach var in $migwhy {
			noi di "`var'"
			reg `var' treated  , ro
			store_est_tpl using sample_table.csv , coef(_cons) name(cmean_`var') all
	}
	
	foreach var in $migwhy {
			noi di "`var'"
			reg `var' treated $unbalancedcov , ro
			store_est_tpl using sample_table.csv , coef(treated) name(`var'_1) all
			reg `var' treated $unbalancedcov $controls , cluster(hh_id)
			store_est_tpl using sample_table.csv , coef(treated) name(`var'_2) all
			reg `var' treated $unbalancedcov $controls i.survey_village  , cluster(hh_id)
			store_est_tpl using sample_table.csv , coef(treated) name(`var'_3) all
	}
	
restore	

table_from_tpl, t(TPL_tabA12.tex) r(sample_table.csv) o(tabA12.tex)





* TABLE A13 
* ----------
 
preserve
use "${data}/migration_prepared", clear
drop if attrition==1


	global migwhy 	migmhr_why1 migmhr_why2 migmhr_why3 migmhr_why4 migmhr_why5 migmhr_why6 migmhr_why7 migmhrall_why1 migmhrall_why2 migmhrall_why3 migmhrall_why4 migmhrall_why5 migmhrall_why6 migmhrall_why7 
	global remitt left_rem1 left_rem2
	collapse (max) migmhr_why* migmhrall_why* treated $controls $remitt survey_village , by(hh_id)

	foreach var in $migwhy migmhr_why {
			noi di "`var'"
			sum left_rem1 if `var'!=0
			local n1=`r(N)'
			insert_into_file using sample_table.csv, key(`var'_left_rem1_mean) 	value(" `r(mean)'") format(%6.2f)
			insert_into_file using sample_table.csv, key(`var'_left_rem1_N) 	value(" `n1'") format(%-9.0g)
			sum left_rem2 if `var'!=0
			local n2=`r(N)'
			insert_into_file using sample_table.csv, key(`var'_left_rem2a_mean) 	value(" `r(mean)'") format(%6.2f)
			insert_into_file using sample_table.csv, key(`var'_left_rem2a_N) 	value(" `n2'") format(%-9.0g)
			sum left_rem2 if `var'!=0 & left_rem1==1
			local n2=`r(N)'
			insert_into_file using sample_table.csv, key(`var'_left_rem2b_mean) 	value(" `r(mean)'") format(%6.2f)
			insert_into_file using sample_table.csv, key(`var'_left_rem2b_N) 	value(" `n3'") format(%-9.0g)
			}
	
restore

table_from_tpl, t(TPL_tabA13.tex) r(sample_table.csv) o(tabA13.tex)

	


* TABLE A14
* ----------

preserve
use "${data}/migration_prepared", clear
drop if attrition==1

	foreach x in $hetero {
			noi di "`var'"
			reg migmhr_dum treated##c.`x' $unbalancedcov $controls i.survey_island , ro
			store_est_tpl using sample_table.csv , coef(_cons) name(cmean_mig_`x') all
			store_est_tpl using sample_table.csv , coef(1.treated) name(hete1_mig_`x') all
			store_est_tpl using sample_table.csv , coef(c.`x') name(hete2_mig_`x') all
			store_est_tpl using sample_table.csv , coef(1.treated#c.`x') name(hete3_mig_`x') all
	}

restore

table_from_tpl, t(TPL_tabA14.tex) r(sample_table.csv) o(tabA14.tex)
		


		
	
* TABLE A15
* ----------

preserve
use "${data}/migration_prepared", clear
drop if attrition==1
						
	** Household 

		* Migmhr Island FE 
		estrat migmhr_dum treated $controls bl_survey_island_* , groups(3) boot(1000) loo_only savegroup
		mat b=e(LOO_C)
		mat c=e(LOO_SE)
		forvalues x=1/3 {
		local temp=(b[1,`x']) 
		insert_into_file using sample_table.csv, key(loo1_hh`x'_beta) value(" `temp'") format(%6.3f)
		local temp=(c[1,`x']) 
		insert_into_file using sample_table.csv, key(loo1_hh`x'_se) value(" `temp'") format(%6.3f)
		sum migmhr_dum if treated==0 & estrat_loo_group==`x'
		local cmean=r(mean)
		insert_into_file using sample_table.csv, key(loo1_hh`x'_cmean) value(" `cmean'") format(%6.3f)
		}
		
		rename estrat_loo_group test_hh1

		* Migmhrall Island FE 
		estrat migmhrall_dum treated $controls bl_survey_island_* , groups(3) boot(1000) loo_only savegroup
		mat b=e(LOO_C)
		mat c=e(LOO_SE)
		forvalues x=1/3 {
		local temp=(b[1,`x']) 
		insert_into_file using sample_table.csv, key(loo2_hh`x'_beta) value(" `temp'") format(%6.3f)
		local temp=(c[1,`x']) 
		insert_into_file using sample_table.csv, key(loo2_hh`x'_se) value(" `temp'") format(%6.3f)
		sum migmhrall_dum if treated==0 & estrat_loo_group==`x'
		local cmean=r(mean)
		insert_into_file using sample_table.csv, key(loo2_hh`x'_cmean) value(" `cmean'") format(%6.3f)
		}
		
		rename estrat_loo_group test_hh2

	** Individual 
		
		drop cfw6
		merge 1:m hh_id using "${data}/migration_indiv_prepared.dta" , force
		drop if _merge!=3
		drop _merge

			
forvalues k=2/3 {

		* Migmhr island FE 
		estrat migmhr treated $controls_indiv bl_survey_island_* , groups(`k') boot(1000) loo_only savegroup
		mat b=e(LOO_C)
		mat c=e(LOO_SE)
		local temp=`k'+1
		forvalues x=1/`temp' {
			local temp=(b[1,`x']) 
			insert_into_file using sample_table.csv, key(loo1_indiv`x'`k'_beta) value(" `temp'") format(%6.3f)
			local temp=(c[1,`x']) 
			insert_into_file using sample_table.csv, key(loo1_indiv`x'`k'_se) value(" `temp'") format(%6.3f)
			sum migmhr if treated==0 & estrat_loo_group==`x'
			local cmean=r(mean)
			insert_into_file using sample_table.csv, key(loo1_indiv`x'`k'_cmean) value(" `cmean'") format(%6.3f)
			}
		
		rename estrat_loo_group test_indivmigmhr`k'	
		
		
		* Migmhrall Island FE 
		estrat migmhrall treated $controls_indiv bl_survey_island_* , groups(`k') boot(1000) loo_only savegroup
		mat b=e(LOO_C)
		mat c=e(LOO_SE)		local temp=`k'+1
		forvalues x=1/`temp' {
			local temp=(b[1,`x']) 
			insert_into_file using sample_table.csv, key(loo2_indiv`x'`k'_beta) value(" `temp'") format(%6.3f)
			local temp=(c[1,`x']) 
			insert_into_file using sample_table.csv, key(loo2_indiv`x'`k'_se) value(" `temp'") format(%6.3f)
			sum migmhrall if treated==0 & estrat_loo_group==`x'
			local cmean=r(mean)
			insert_into_file using sample_table.csv, key(loo2_indiv`x'`k'_cmean) value(" `cmean'") format(%6.3f)
			}
		
		rename estrat_loo_group test_indivmigmhrall`k'	

}

restore
		
table_from_tpl, t(TPL_tabA15.tex) r(sample_table.csv) o(tabA15.tex) // stars should be added by hand







** Adults in the household
preserve
use "${data}/migration_prepared", clear
drop if attrition==1
tab bl_working_age_adult_n
restore	

** Uneven roll-out
preserve
use "${data}/migration_prepared", clear
drop if attrition==1
reg bl_treated_tot_cont bl_mig_willingness , cluster(ZIP)
reg bl_treated_tot_cont bl_ihsconsopea  , cluster(ZIP)
reg phase1 bl_mig_willingness 
reg phase1 bl_ihsconsopea 
restore



